package com.ljx.itbbs.server.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ljx.itbbs.common.domain.entity.UserEntity;
import com.ljx.itbbs.common.domain.vo.resp.CursorResp;
import com.ljx.itbbs.server.article.dao.CommentReplyDao;
import com.ljx.itbbs.server.article.domain.entity.CommentEntity;
import com.ljx.itbbs.server.article.domain.entity.CommentReplyEntity;
import com.ljx.itbbs.server.article.domain.vo.req.ArticleCommentReq;
import com.ljx.itbbs.server.article.domain.vo.resp.CommentReplyResp;
import com.ljx.itbbs.server.article.domain.vo.resp.CommentResp;
import com.ljx.itbbs.server.article.service.CommentReplyService;
import com.ljx.itbbs.server.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CommentReplyServiceImpl implements CommentReplyService {

    @Autowired
    private CommentReplyDao commentReplyDao;

    @Autowired
    private UserService userService;

    @Override
    public CursorResp<CommentReplyResp> reply(ArticleCommentReq req) {
        // 查询评论分页
        CursorResp<CommentReplyEntity> page = commentReplyDao.getCommentReplyList(req);
        List<CommentReplyEntity> commentList = page.getList();
        // 获取评论用户ID和回复用户ID
        Set<Long> uIds = commentList.stream().map(CommentReplyEntity::getUid).collect(Collectors.toSet());
        uIds.addAll(commentList.stream().map(CommentReplyEntity::getReplyUid).collect(Collectors.toSet()));
        // 封装组合
        Map<Long, UserEntity> userMap = userService.getUserInfoByIds(new ArrayList<>(uIds));
        List<CommentReplyResp> records = commentList.stream().map(item -> {
            CommentReplyResp commentResp = BeanUtil.copyProperties(item, CommentReplyResp.class);
            return setUserInfo(item, userMap, commentResp);
        }).collect(Collectors.toList());

        return CursorResp.<CommentReplyResp>builder()
                .list(records)
                .isLast(page.isLast())
                .cursor(page.getCursor())
                .build();
    }

    private CommentReplyResp setUserInfo(CommentReplyEntity item, Map<Long, UserEntity> userMap, CommentReplyResp commentResp) {
        UserEntity user = userMap.get(item.getUid());
        commentResp.setNickname(user.getNickname());
        commentResp.setAvatar(user.getAvatar());
        if (Objects.nonNull(item.getReplyUid())){
            user = userMap.get(item.getReplyUid());
            commentResp.setReplyNickname(user.getNickname());
            commentResp.setReplyAvatar(user.getAvatar());
        }

        return commentResp;
    }

    @Override
    public void saveCommentReply(CommentReplyEntity commentReplyEntity) {
        commentReplyDao.save(commentReplyEntity);
    }

    @Override
    public void likeCommentReply(Long id) {
        commentReplyDao.likeCommentReply(id);
    }

    @Override
    public void reduceCommentReplyLikeNum(Long objId) {
        commentReplyDao.reduceCommentReplyLikeNum(objId);
    }
}
